Shiny is an R package that makes it easy to build interactive web apps straight from R. You can host standalone apps on a webpage or embed them in R Markdown documents or build dashboards. You can also extend your Shiny apps with CSS themes, htmlwidgets, and JavaScript actions.
Shiny is most often used with the packages “shinydashboard” or “flexdashboard” to build interactive dashboards. We will walk through how to use both shinydashboard and flexdashboard to give you options when using shiny.
3 Main parts:
library("shiny")
library("tidyverse")
selectInput(
inputId = "object", # This is the name you described the object elsewhere in the application
label = "Favorite Character", # The label you want to show to the user
choices = c(starwars$name) # List the choices the user has to pick from
)
Functions that you use in your application’s server side code, assigning them to outputs that appear in your user interface.
library(shiny)
There were 42 warnings (use warnings() to see them)
library(tidyverse)
Registered S3 methods overwritten by 'dbplyr':
method from
print.tbl_lazy
print.tbl_sql
[30m-- [1mAttaching packages[22m --------------------------------------- tidyverse 1.3.0 --[39m
[30m[32mv[30m [34mggplot2[30m 3.3.1 [32mv[30m [34mpurrr [30m 0.3.4
[32mv[30m [34mtibble [30m 3.0.1 [32mv[30m [34mdplyr [30m 1.0.0
[32mv[30m [34mtidyr [30m 1.1.0 [32mv[30m [34mstringr[30m 1.4.0
[32mv[30m [34mreadr [30m 1.3.1 [32mv[30m [34mforcats[30m 0.5.0[39m
[30m-- [1mConflicts[22m ------------------------------------------ tidyverse_conflicts() --
[31mx[30m [34mdplyr[30m::[32mfilter()[30m masks [34mstats[30m::filter()
[31mx[30m [34mdplyr[30m::[32mlag()[30m masks [34mstats[30m::lag()[39m
library(shinydashboard)
Attaching package: 㤼㸱shinydashboard㤼㸲
The following object is masked from 㤼㸱package:graphics㤼㸲:
box
header <- dashboardHeader(
title = "Example Dashboard Header",
titleWidth = 300
)
sidebar <- dashboardSidebar(
selectInput(
inputId = "name",
label = "Favorite Character",
choices = c(starwars$name)
)
)
body <- dashboardBody(
textOutput("name")
)
After you code the sidebar and body, you can then create the User Interface, or “ui”. The User Interface is built from a header, sidebar, and body. When you modify any of these options, the easiest way is to save it as an object called “header”, “sidebar”, or “body”. The user interface is most often coded into the dashboardPage() function.
ui <- dashboardPage(header = header,
sidebar = sidebar,
body = body
)
server <- function(input, output) {
output$name <- renderText({
input$name
})
}
shinyApp(ui, server)
There are two main types of layouts that shiny uses. The first is a “row” based layout, while the other is a “column” based layout. You can use either one, or even a mix of the two. The way to create the layouts is by using the “fluidRow()” function.
These functions are used inside of the dashboardBody() section. Each time “fluidRow()” is called, a new row is created.
body <- dashboardBody(
fluidRow(
# Row 1
box(
width = 12, # 12 width spans the entire width of the screen
title = "Regular Box, Row 1",
"Text inside of box"
)),
fluidRow(
# Row 2
box(
width = 12,
title = "Regular Box, Row 2",
"Text inside of box 2"
)
)
)
ui <- dashboardPage(header = dashboardHeader(),
sidebar = dashboardSidebar(),
body = body
)
ui
The next layout option is to build columns. The difference in using columns instead of rows is to use the “column()” function inside of “fluidRow()”.
You can set the width of the column, but when you insert a box or chart, the width needs to be “NULL”.
body <- dashboardBody(
fluidRow(
# Column 1
column(width = 6,
infoBox(
width = NULL, # The width must be "NULL" when using a column layout
title = "Regular Box, Column 1",
"Text inside of box"
)
),
column(width = 6,
# Column 2
infoBox(
width = NULL, # The width must be "NULL" when using a column layout
title = "Regular Box, Column 2",
"Text inside of box"
)
)
)
)
ui <- dashboardPage(header = dashboardHeader(),
sidebar = dashboardSidebar(),
body = body
)
shinyApp(ui, server)
Note: To create a new row, call another fluidRow() function.
body <- dashboardBody(
fluidRow(
# Row 1
box(
width = 12, # The width must be "NULL" when using a column layout
title = "Regular Box, Row 1",
"Text inside of box"
),
),
fluidRow(
column(width = 6,
# Column 2
infoBox(
width = NULL, # The width must be "NULL" when using a column layout
title = "Regular Box, Row 2, Column 1",
subtitle = "Text inside of box"
)
),
column(width = 6,
infoBox(
width = NULL,
title = "Regular Box, Row 2, Column 2",
subtitle = "Text inside of box"
)
)
)
)
ui <- dashboardPage(header = dashboardHeader(),
sidebar = dashboardSidebar(),
body = body
)
shinyApp(ui, server)